﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace Agiltastic
{
    public partial class fBreed : FormBase
    {
        private BreedDao _breedDao = new BreedDao();
        private List<Breed> _listBreed = new List<Breed>();
        private Breed _currentSelected;
      

        public fBreed()
        {
            InitializeComponent();
        }

        private void fBreed_Load(object sender, EventArgs e)
        {
            try
            {
                 LoadGrid();
                EnableControls(false);
                CurrentMode = Mode.DoNothing;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

        private void LoadGrid()
        {
            _listBreed = _breedDao.GetAllBreed();
            dgvBreed.DataSource = _listBreed;
        }

        private void EnableControls(bool b)
        {
            txtName.Enabled = b;
            btnSave.Enabled = b;
        }

        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            dgvBreed.DataSource = _listBreed.FindAll(c => c.Name.ToUpperInvariant().Contains(txtFilter.Text.ToUpperInvariant()));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            _currentSelected = new Breed();
            CurrentMode = Mode.Add;
            EnableControls(true);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvBreed.SelectedCells.Count > 0)
            {
                var name = (String) dgvBreed.SelectedCells[0].Value;
                if(name != "")
                {
                    _currentSelected = _listBreed.Find(c => c.Name == name);
                    txtName.Text = _currentSelected.Name;
                    EnableControls(true);
                    CurrentMode = Mode.Edit;
                }
            }
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtName.Text))
                MessageBox.Show("The name cannot be empty");
            else
            {
                if(CurrentMode == Mode.Edit)
                {
                    _currentSelected.Name = txtName.Text;
                    _breedDao.Update(_currentSelected);
                }

                if (CurrentMode == Mode.Add)
                {
                    _currentSelected.Name = txtName.Text;
                    _breedDao.Save(_currentSelected);
                }
                LoadGrid();
                EnableControls(false);
            }
        }
    }
}
